/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elookinto.wordlist;

import java.io.FileInputStream;
import java.io.*;
import java.sql.*;
import java.util.HashSet;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.util.PDFTextStripper;

/**
 *
 * @author zw251y
 */
public class PDFExtractor {

    public static void main(String s[]) throws Exception {
        PDFParser pdf = new PDFParser(new FileInputStream("C:/Users/zw251y/Documents/NetBeansProjects/wordlist_chinese/data/1/1.pdf"));
        pdf.parse();
        PDFTextStripper ps = new PDFTextStripper();
        String text = ps.getText(pdf.getPDDocument());
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter
  (new FileOutputStream("c:/temp/1.txt"),"UTF8"));
 
  out.write(text);
 
 out.close();
 
        System.out.println (text);
        pdf.getPDDocument().close();
    }

    public static void grade1(String s, int grade) throws Exception {
        PDFParser pdf = new PDFParser(new FileInputStream(s));
        pdf.parse();
        PDFTextStripper ps = new PDFTextStripper();
        String text = ps.getText(pdf.getPDDocument());
        pdf.getPDDocument().close();
        //System.out.println(text);
        int beginWeek = 0;
        int week = 1;
        while (true) {
            String sweek = week<10? "0"+week:""+week;
            String listName = "Grade" + grade + "_" + "week" + sweek;
              System.out.println(listName);
            beginWeek = text.indexOf("Week " + week, beginWeek);
            if (beginWeek == -1) {
                break;
            }
            int one = text.indexOf("1.", beginWeek);
            //int six = text.indexOf("6.", one);
            int endWeek = text.indexOf("Week", one);
            if (endWeek == -1) {
                endWeek = text.length();
            }
            String weekWords = (text.substring(one, endWeek));
            //System.out.println(weekWords);
            insert_words(weekWords, listName);
            week++;
            beginWeek = endWeek;
        }

    }

    public static void grade6(String s) throws Exception {
        PDFParser pdf = new PDFParser(new FileInputStream(s));
        pdf.parse();
        PDFTextStripper ps = new PDFTextStripper();
        String text = ps.getText(pdf.getPDDocument());
        pdf.getPDDocument().close();
        //System.out.println(text);
        int beginWeek = 0;
        int week = 1;
        while (true) {
            String sweek = week<10? "0"+week:""+week;
            String listName = "Grade6_" + "week" + sweek;
             System.out.println(listName);
            beginWeek = text.indexOf("Week " + week, beginWeek);
            if (beginWeek == -1) {
                break;
            }
            int one = text.indexOf("THURSDAY", beginWeek) + 8;
            one = text.indexOf("\n", one);
            one = text.indexOf("\n", one + 2);
            int endWeek = text.indexOf("Week " + (week + 1), one);
            if (endWeek == -1) {
                endWeek = text.length();
            }
            String weekWords = (text.substring(one, endWeek));
            System.out.println(weekWords);
           // insert_words(weekWords, listName);
            //System.out.println(weekWords);
            week++;
            beginWeek = endWeek;
        }

    }
    static HashSet hs = new HashSet() {

        {
            this.add("w");
            this.add("d");
            this.add("z");
            this.add("ee");
            this.add("comments");
            this.add("e");
            this.add("wd");
        }
    };

    public static void insert_words(String text, String listName) throws Exception {
        String[] words = text.split("\\w");
        int count = 0;
        Connection con = Util.getCon();
        PreparedStatement st = con.prepareStatement("insert into wordlist (word, listname, uid, insert_time) values(?,?,?, current_timestamp) ");
        for (String word : words) {
            word = word.toLowerCase();
            if (word.trim().length() == 0 || hs.contains(word)) {
                continue;
            }
            st.setString(1, word);
            st.setString(2, listName);
            st.setString(3, "admin");
            try {
                st.execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
            count++;
        }
        st.close();
        con.close();
        System.out.println("count=" +count);
    }
}
